﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DemoForm
{
    public partial class BallForm : Form
    {
        public static int MaxX  = 0;
        public static int MaxY = 0;
        public BallForm()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
            MaxX = panel1.Width;
            MaxY = panel1.Height;
        }

        private List<Ball> bList = new List<Ball>();

        private void btnShot_Click(object sender, EventArgs e)
        {
            var ball = RandomBall();
            DrawBall(ball);
            bList.Add(ball);
        }
        private Color[] Colors = new Color[] { Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Blue, Color.Brown, Color.Purple, Color.Pink };
        private Ball RandomBall()
        {
            Random rand = new Random(Guid.NewGuid().GetHashCode());
            var ball = new Ball() 
            {
                Size = rand.Next(20,40),
                SpeedX = rand.Next(1,5),
                SpeedY = rand.Next(1,5),
                Color = Colors[rand.Next(0,Colors.Length)]
            };
            ball.Location = new Point(ball.Size + 1, ball.Size + 1);

            return ball;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                timer1.Enabled = false;
                panel1.Refresh();
                for (int i = 0; i < bList.Count; i++)
                {
                    var b = bList[i];

                    b.Move(bList);

                    DrawBall(b);
                }
            }
            finally
            {
                timer1.Enabled = true;
            }
        }

        private void DrawBall(Ball b)
        {
            var g = panel1.CreateGraphics();
            g.DrawEllipse(new Pen(b.Color, 2), b.Location.X - b.Size, b.Location.Y - b.Size, 2 * b.Size, 2 * b.Size);
        }
    }

    public class Ball
    {
        public Point Location { get; set; }

        public int Size { get; set; }

        public int SpeedX { get; set; }

        public int SpeedY { get; set; }

        public Color Color { get; set; }

        internal void Move(List<Ball> bList)
        {
            foreach (var b in bList)
            {
                if (b.Location != this.Location)
                {
                    var dis = GetDistance(this.Location, b.Location);
                    if (dis < this.Size + b.Size + this.SpeedY)
                    {
                        this.SpeedX = 0 - SpeedX;
                        this.SpeedY = 0 - SpeedY;
                    }
                }
                if (this.Location.X + SpeedX >= BallForm.MaxX - Size || this.Location.X + SpeedX <= Size)
                    SpeedX = 0 - SpeedX;

                if (this.Location.Y + SpeedY >= BallForm.MaxY -Size || this.Location.Y + SpeedY <= Size)
                    SpeedY = 0 - SpeedY;

                this.Location = new Point(this.Location.X + SpeedX, this.Location.Y + SpeedY);
            }
        }

        private void SetNewSpeed(Ball block)
        {
            
        }

        private double GetDistance(Point A, Point B)
        {
            return Math.Pow(Math.Pow(A.X - B.X, 2) + Math.Pow(A.Y - B.Y, 2), 0.5);
        }
    }
}
